using System;
using System.Collections.Generic;
using System.Text;
//Needed for the InterfaceKit class, phidget base classes, and the PhidgetException class
using Phidgets;
//Needed for the event handling classes 
using Phidgets.Events;
namespace LorraineSec.Libraries.Sensors
{
    public class PhidgetIR_Lib : Sensor
    {
        /// <summary>
        /// Library for interfacing with the interfacekit that receives signal from IR sensor via USB.
        /// </summary>
        private static InterfaceKit ifKit = null;
        private static int IRLibInitCounter = 0;
       
        private int IRInitIndex;

        public PhidgetIR_Lib() { }
        
        public override bool initiate()
        {
             try
            {
                 //Only initialize InterKit on first Library init
                if (ifKit == null)
                {
                    //Initialize the InterfaceKit object
                    ifKit = new InterfaceKit();

                    //Hook the basic event handlers
                    ifKit.Attach += new AttachEventHandler(ifKit_Attach);
                    ifKit.Detach += new DetachEventHandler(ifKit_Detach);
                    ifKit.Error += new ErrorEventHandler(ifKit_Error);

                    //Hook the phidget spcific event handlers
                    ifKit.InputChange += new InputChangeEventHandler(ifKit_InputChange);
                    ifKit.OutputChange += new OutputChangeEventHandler(ifKit_OutputChange);
                    ifKit.SensorChange += new SensorChangeEventHandler(ifKit_SensorChange);
                    //Open the object for device connections
                    ifKit.open();
                }
                //Set Init Counter & Increment
                IRInitIndex = ++IRLibInitCounter;
                return true;
            }
             catch (Exception)
             {
                 return false;
             }
        }

      

        //Attach event handler...Display the serial number of the attached InterfaceKit 
        //to the console
        void ifKit_Attach(object sender, AttachEventArgs e)
        {
            Console.WriteLine("InterfaceKit {0} attached!", 
                                e.Device.SerialNumber.ToString());
        }

        //Detach event handler...Display the serial number of the detached InterfaceKit 
        //to the console
        void ifKit_Detach(object sender, DetachEventArgs e)
        {
            Console.WriteLine("InterfaceKit {0} detached!", 
                                e.Device.SerialNumber.ToString());
        }

        //Error event handler...Display the error description to the console
        void ifKit_Error(object sender, ErrorEventArgs e)
        {
            Console.WriteLine(e.Description);
        }

        //Input Change event handler...Display the input index and the new value to the 
        //console
        void ifKit_InputChange(object sender, InputChangeEventArgs e)
        {
            if (IRInitIndex == e.Index)
            {
                notify(new IRFrame(this, e.Index.ToString() + e.Value.ToString()));
            }
        }

        //Output change event handler...Display the output index and the new valu to 
        //the console
        void ifKit_OutputChange(object sender, OutputChangeEventArgs e)
        {
            Console.WriteLine("Output index {0} value {0}", e.Index, e.Value.ToString());
        }

        //Sensor Change event handler...Display the sensor index and it's new value to 
        //the console
        void ifKit_SensorChange(object sender, SensorChangeEventArgs e)
        {
            Console.WriteLine("Sensor index {0} value {1}", e.Index, e.Value);
        }

        //Define the frame class for this library
        public class IRFrame : SensorFrame
        {
            public IRFrame(PhidgetIR_Lib parent)
                : base(parent)
            {
            }
            public IRFrame(PhidgetIR_Lib parent, string val)
                : base(parent)
            {
                Data = val;
            }
        }

        public override string ToString()
        {
            return GetType().Name;
        }
    }
}
